using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using System.Collections.Generic;
using ECommerce.Library;
using System.IO;
using Ext.Net;
using eShop.Model;
using eShop.Business;

public partial class admin_product_category : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            LoadProductCategory();
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindCategories(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    protected void imgSearchButton_Click(object sender, EventArgs e)
    {

    }

    private void BindCategories(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<Category> data = null;
        int totalCount = 0;

        if (this._useSearch)
        {
            string _searchCategoryName = txtToolbarSearch.Text;
            int _parentCategoryId;
            if (!Int32.TryParse(ddlToolbarCategories.SelectedItem.Value, out _parentCategoryId))
                _parentCategoryId = SiteUtility.SuperRootCategoryId;

            if (!string.IsNullOrEmpty(_searchCategoryName) && !(_parentCategoryId == SiteUtility.SuperRootCategoryId))
            {
                data = CategoryManager.GetBackendCategoryListByParentCategoryIdAndNameSearch(_parentCategoryId, _searchCategoryName, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByParentCategoryIdAndNameCount(_parentCategoryId, _searchCategoryName, false);
            }
            else if (!string.IsNullOrEmpty(_searchCategoryName))
            {
                data = CategoryManager.GetBackendCategoryListByNameSearch(_searchCategoryName, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByNameCount(_searchCategoryName, false);
            }
            else if (!(_parentCategoryId == SiteUtility.SuperRootCategoryId))
            {
                data = CategoryManager.GetBackendCategoryListByParentCategoryIdSearch(_parentCategoryId, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByParentCategoryIdCount(_parentCategoryId, false);
            }
            else
            {
                data = CategoryManager.GetBackendCategoryList(false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryCount(false);
            }

            Session["_useSearch"] = null;
        }
        else
        {
            //data = ProductCategory.GetBackendCategoryList(false, this._sortBy, firstItem, pageSize);
            data = CategoryManager.GetBackendCategoryList(false, sort, start, limit);
            totalCount = CategoryManager.GetBackendCategoryCount(false);    
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    protected void LoadProductCategory()
    {
        //ddlParent.Items.Clear();
        ReadWriteCustomCollection<KeyValuePair<string, string>> productCategoryTree = CategoryManager.GetSubCategoryTree(SiteUtility.SuperRootCategoryId);
        foreach (KeyValuePair<string, string> productCategory in productCategoryTree)
        {
            //ddlParent.Items.Add(new System.Web.UI.WebControls.ListItem(productCategory.Key, productCategory.Value));
            ddlToolbarCategories.Items.Add(new Ext.Net.ListItem(productCategory.Key, productCategory.Value));
        }
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateCategory(int id, string categoryName, string displayOrder, bool isActive)
    {
        Category category = CategoryManager.GetById(id);
        category.CategoryName = categoryName;
        category.DisplayOrder = displayOrder;
        category.IsActive = isActive;
        CategoryManager.Update(category);
        GridPanel1.Store.Primary.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteCategories(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        CategoryManager.DeleteCategories(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchCategory()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
